package com.cskaoyan.service.wx.goods;

import com.cskaoyan.bean.MarketCategory;
import com.cskaoyan.bean.MarketCategoryExample;
import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.MarketGoodsExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.vo.wx.goods.categoryvo.CateGoryListVO;
import com.cskaoyan.bean.vo.wx.goods.categoryvo.CategoryVO;
import com.cskaoyan.mapper.MarketCategoryMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: bwj
 * @Date: 2022/09/09/12:00
 * @Description:
 */
@Service
public class WxGoodsServiceImpl2 implements WxGoodsService2 {

    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    @Transactional
    public CategoryVO categoryInfo(Integer id) {
        // 通过传进来的categoryId拿到pid
        // 查出所有的类目信息
        MarketCategory marketCategory = marketCategoryMapper.selectByPrimaryKey(id);
        // 拿到所找出类目的pid值
        Integer pid = marketCategory.getPid();
        /*
        表中有两种pid值： 1. pid等于0的值 为一级类目
                        2. pid不等于0的值 为二级类目 二级类目的pid值等于以及类目的id值
         */
        if (pid == 0) {
            //pid等于0 直接封装为parentCategory
            MarketCategory parentCategory = marketCategoryMapper.selectByPrimaryKey(id);
            MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
            criteria.andPidEqualTo(id);
            List<MarketCategory> brotherCategory = marketCategoryMapper.selectByExample(marketCategoryExample);
            MarketCategory currentCategory = brotherCategory.get(0);
            CategoryVO categoryVO = new CategoryVO(brotherCategory, currentCategory, parentCategory);
            return categoryVO;

        }else {
            MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteriaCategory = marketCategoryExample.createCriteria();
            criteriaCategory.andPidEqualTo(pid);
            //criteriaCategory.andIdEqualTo(pid);
            //拿到所有id为等于categoryId的数据
            List<MarketCategory> brotherCategory = marketCategoryMapper.selectByExample(marketCategoryExample);
            //得到第一条数据将它放入currentCategory
            //MarketCategory currentCategory = brotherCategory.get(0);
            MarketCategory currentCategory = marketCategoryMapper.selectByPrimaryKey(id);
            //通过currentCategory拿到当前id的pid 去获得对应的parentCategory
            Integer parentId = currentCategory.getId();
            MarketCategory parentCategory = marketCategoryMapper.selectByPrimaryKey(parentId);
            //封装数据
            CategoryVO categoryVO = new CategoryVO(brotherCategory, currentCategory, parentCategory);
            return categoryVO;
        }
    }
}
